#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>

// 自己的头文件
#include "test/my_test.h"
#include "test/my_win.h"

#define DISP_BUF_SIZE (480 * 1024)

// 全局标签对象，用于显示时间
lv_obj_t *time_label = NULL;

// 定时器回调函数：更新时间显示
static void update_time(lv_timer_t *timer)
{
    // 获取当前时间
    struct timeval tv;
    struct tm *tm_info;
    char time_str[64];

    gettimeofday(&tv, NULL);
    tm_info = localtime(&tv.tv_sec);

    // 格式化为 "HH:MM:SS" 格式
    strftime(time_str, sizeof(time_str), "%H:%M:%S", tm_info);
    
    // 更新标签文本
    if(time_label) 
    {
        lv_label_set_text(time_label, time_str);
        lv_obj_invalidate(time_label); // 确保界面刷新
    }
}

// 创建时间显示功能
void create_time_display(void)
{
    // 创建标签对象
    time_label = lv_label_create(lv_scr_act());
    
    // 设置文本对齐方式：右对齐
    lv_obj_set_style_text_align(time_label, LV_TEXT_ALIGN_RIGHT, 0);
    
    // 定位到右上角，边距为 10px
    lv_obj_align(time_label, LV_ALIGN_TOP_RIGHT, -10, 10);
    
    // 创建定时器：每秒更新一次时间
    lv_timer_create(update_time, 1000, NULL);
}

int main(void)
{
    /* LVGL 初始化 */
    lv_init();

    /* 输出设备初始化及注册 */
    fbdev_init();
    
    /* 初始化显示缓冲区 */
    static lv_color_t buf[DISP_BUF_SIZE];
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
    
    /* 初始化并注册显示驱动 */
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    
    lv_disp_t * disp = lv_disp_drv_register(&disp_drv);
    if(!disp) 
    {
        printf("Failed to register display driver!\n");
        return -1;
    }

    /* 输入设备初始化及注册 */
    evdev_init();
    
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1);
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;
    indev_drv_1.read_cb = evdev_read;
    
    lv_indev_t * indev = lv_indev_drv_register(&indev_drv_1);
    if(!indev) 
    {
        printf("Failed to register input device!\n");
        return -1;
    }

    /* 创建界面元素 */
    //显示标题：公告
    lv_tao_label_1();
    //显示公告1
    lv_tao_label_2();
    //显示公告2
    lv_tao_label_3();

    //广告
    font_test();

    //界面
    show_main_win();

    //时间
    create_time_display();

    /* 记录启动时间 */
    struct timeval start_time;
    gettimeofday(&start_time, NULL);
    
    while(1) 
    {
        /* 计算时间增量 */
        static uint32_t last_tick = 0;
        struct timeval now;
        gettimeofday(&now, NULL);
        
        uint32_t now_tick = (now.tv_sec - start_time.tv_sec) * 1000 + 
                           (now.tv_usec - start_time.tv_usec) / 1000;
        
        uint32_t tick_diff = now_tick - last_tick;
        if(tick_diff > 0) {
            lv_tick_inc(tick_diff);
            last_tick = now_tick;
        }
        
        /* 处理 LVGL 任务 */
        lv_task_handler();
        
        /* 延时，避免 CPU 占用过高 */
        usleep(5000); // 5ms
    }

    return 0;
}